package com.nanuminc.market.today;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.nanuminc.market.R;

public class TodayLocation_spinner_ngoc extends Activity implements OnItemSelectedListener {

	private Spinner spinner1, spinner2;
	private Button btnSubmit;
	ArrayAdapter<String> adapter1, adapter2;
  //  String Province[] = { "Seoul", "Daegu", "Gyeonggi", "Incheon", "Ulsan", "Gwangju", "Sejong",
   //         "Busan", "Jeju"};
    String Province[] = { "Seoul", "Daegu", "Gyeonggi", "Incheon"};
    String Seoul[] = { "Seoul 1", "Seoul 2", "Seoul 3", "Seoul 4", "Seoul 5", "Seoul 6", "Seoul 7",
            "Seoul 8", "Seoul 9"};
    String Daegu[] = { "Daegu 1", "Daegu 2", "Daegu 3", "Daegu 4", "Daegu 5", "Daegu 6", "Daegu 7",
            "Daegu 8", "Daegu 9"};
    String Gyeonggi[] = { "Gyeonggi 1", "Gyeonggi 2", "Gyeonggi 3", "Gyeonggi 4", "Gyeonggi 5", "Gyeonggi 6", "Gyeonggi 7",
            "Gyeonggi 8", "Gyeonggi 9"};
    String Incheon[] = { "Incheon 1", "Incheon 2", "Incheon 3", "Incheon 4", "Incheon 5", "Incheon 6", "Incheon 7",
            "Incheon 8", "Incheon 9"};
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.todaylocation_ngoc);
		addItemsOnSpinner2();
		addListenerOnButton();
		addListenerOnSpinnerItemSelection();
	}
	
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
	//add items into spinner dynamically
	public void addItemsOnSpinner2() {

		spinner1 = (Spinner) findViewById(R.id.spinner1);
		adapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, Province); 
		adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner1.setAdapter(adapter1);
	}
 
	
	public void addListenerOnSpinnerItemSelection(){

		spinner1 = (Spinner) findViewById(R.id.spinner1);
	//	spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());		
		spinner2 = (Spinner) findViewById(R.id.spinner2);
    	
		spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
	        @Override
	        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {        	
	            if(position==0){ //Seoul province is sellected on 1st spinner
	        		adapter2 = new ArrayAdapter<String>(parentView.getContext(),     // be careful: if you change parentView.getContext() to this, it dont work.
	                        android.R.layout.simple_spinner_item, Seoul);  //add all the districs of Seoul
		    		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		    		spinner2.setAdapter(adapter2);
	            }	
	            else if(position==1){ //Daegu province is sellected on 1st spinner
	        		adapter2 = new ArrayAdapter<String>(parentView.getContext(),     // be careful: if you change parentView.getContext() to this, it dont work.
	                        android.R.layout.simple_spinner_item, Daegu); //add all the cities of Daegu
		    		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		    		spinner2.setAdapter(adapter2);
	            }    
	            else if(position==2){
	        		adapter2 = new ArrayAdapter<String>(parentView.getContext(),     // be careful: if you change parentView.getContext() to this, it dont work.
	                        android.R.layout.simple_spinner_item, Gyeonggi); 
		    		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		    		spinner2.setAdapter(adapter2);
	            }
	            else if(position==3){
	        		adapter2 = new ArrayAdapter<String>(parentView.getContext(),R.array.category,     // be careful: if you change parentView.getContext() to this, it dont work.
	                        android.R.layout.simple_spinner_item); 
		    		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		    		spinner2.setAdapter(adapter2);
	            }
	        }
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }

	    });
	    
	}
	

	//get the selected dropdown list value
	public void addListenerOnButton() {

		spinner1 = (Spinner) findViewById(R.id.spinner1);
		spinner2 = (Spinner) findViewById(R.id.spinner2);
		
		btnSubmit = (Button) findViewById(R.id.btnSearch);

		btnSubmit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Toast.makeText(TodayLocation_spinner_ngoc.this,
						"OnClickListener : " + 
						"\nSpinner 1 : " + String.valueOf(spinner1.getSelectedItem()) +
						"\nSpinner 2 : " + String.valueOf(spinner2.getSelectedItem()),
						Toast.LENGTH_SHORT).show();
			}
		});
	}


}